#!/usr/bin/env python
# coding=utf-8

from lib.boson_api import BosonAPI

class User:

    def __init__(self, name):
        self.name = name
        self.data_list = []
        self.nlp = BosonAPI()

    def append_data(self, data):
        self.data_list.append(data)

    def get_data_list(self):
        return self.data_list

    def match_data(self, msg):
        m_datas = []
        for d in self.data_list:
            if self._match_rule(msg, d):
                m_datas.append(d)

        return m_datas
                
    def _match_rule(self, msg, data):
        print("calling boson api")
        results = self.nlp.keywords([msg, data.content])
        print("finish calling boson api")
        MIN_WEIGHT = 0.3
        keys_1 = []
        keys_2 = []
        for r in results[0]:
            if r[0] < MIN_WEIGHT:
                continue

            keys_1.append(r[1])

        for r in results[1]:
            if r[0] < MIN_WEIGHT:
                continue

            keys_2.append(r[1])
        
        inter = set(keys_1).intersection(set(keys_2))
        if len(inter):
            return True

        return False


    def delete_data(self, msg):
        print("before del: ", len(self.data_list))

        m_datas = []
        for d in self.data_list:
            if self._match_rule(msg, d):
                m_datas.append(d)

        for d in m_datas:
            self.data_list.remove(d)

        print("after del: ", len(self.data_list))

        return m_datas
